package com.wzhq.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsfUtil {
    private static final Logger LogUtil = LoggerFactory.getLogger(JsfUtil.class);

    DataSource ds;
    java.sql.Connection Conn = null;
    java.sql.ResultSet rs = null;
    Statement stmt;


    public static enum PersistAction {

        CREATE,
        DELETE,
        UPDATE
    }

    public static Object autoGetterAndSetter(Object src, Object target) throws Exception {

        Method[] sms = src.getClass().getMethods();     // 原始类方法 srcMethod  
        Method[] tms = target.getClass().getMethods();  // 目标类防范 targetMethod  
        for (Method sm : sms) {
            if (sm.getName().startsWith("get")) {       // 原始类的 getter  
                String attrName = sm.getName().substring(3);// 属性  
                for (Method tm : tms) {// 遍历目标类  
                    // LOGGER.info("set" + attrName);
                    //LOGGER.info(tm.getName());
                    if (("set" + attrName).equals(tm.getName())) {// 执行目标类的指定attrName的setter  
                        //LOGGER.info("set" + attrName);
                        tm.invoke(target, sm.invoke(src));
                    }
                }
            }
        }
        return target;
    }

    /**
     * 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
     *
     * @param obj
     * @return
     */
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }

        if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        }

        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        }

        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNullOrEmpty(object[i])) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    public static int getAge(Date dateOfBirth) {
        int age = 0;
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        if (dateOfBirth != null) {
            now.setTime(new Date());
            born.setTime(dateOfBirth);
            if (born.after(now)) {
                throw new IllegalArgumentException(
                        "Can't be born in the future");
            }
            age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
            if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
                age -= 1;
            }
        }
        return age;
    }

    public static Date getDateTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        return cal.getTime();
    }
}
